Page({
  data: {
    likeStatus: null, // 点赞状态
    likedList: [] // 已点赞新闻列表
  },

  // 查看点赞状态
  checkLikeStatus: function () {
    const userId = 1;
    const newsId = 1;
    const baseUrl = 'http://39.104.24.231:8082';
    const checkUrl = baseUrl + '/agree/check';

    wx.request({
      url: checkUrl,
      method: 'POST',
      data: {
        USER_ID: userId,
        NEWS_ID: newsId
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          this.setData({
            likeStatus: res.data.data
          });
          wx.showToast({
            title: '获取点赞状态成功',
            icon:'success'
          });
        } else {
          wx.showToast({
            title: `获取点赞状态失败: ${res.data.message}`,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: `请求失败: ${err.errMsg}`,
          icon: 'none'
        });
      }
    });
  },

  // 改变点赞状态
  changeLikeStatus: function () {
    const userId = 1;
    const newsId = 1;
    const currentStatus = this.data.likeStatus;
    const newStatus =!currentStatus; // 取反当前状态
    const baseUrl = 'http://39.104.24.231:8082';
    const changeUrl = baseUrl + '/agree/agreeOrDisAgree';

    wx.request({
      url: changeUrl,
      method: 'POST',
      data: {
        USER_ID: userId,
        NEWS_ID: newsId,
        status: newStatus
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          this.setData({
            likeStatus: newStatus
          });
          wx.showToast({
            title: newStatus? '点赞成功' : '取消点赞成功',
            icon:'success'
          });
        } else {
          wx.showToast({
            title: `操作失败: ${res.data.message}`,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: `请求失败: ${err.errMsg}`,
          icon: 'none'
        });
      }
    });
  },

  // 请求已点赞列表
  getLikedList: function () {
    const userId = 1; // 假设用户 ID
    const baseUrl = 'http://39.104.24.231:8082';
    const listUrl = baseUrl + '/agree/getList';

    wx.request({
      url: listUrl,
      method: 'POST',
      data: {
        USER_ID: userId
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          this.setData({
            likedList: res.data.data
          });
          wx.showToast({
            title: '获取已点赞列表成功',
            icon:'success'
          });
        } else {
          wx.showToast({
            title: `获取已点赞列表失败: ${res.data.message}`,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: `请求失败: ${err.errMsg}`,
          icon: 'none'
        });
      }
    });
  }
});